package top.lyc829.zzl_android.ui.thing;

import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.next.easynavigation.adapter.ViewPager2Adapter;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.textview.badge.Badge;
import com.xuexiang.xui.widget.textview.badge.BadgeView;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ThingFragmentBinding;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.login.LoginActivity;
import top.lyc829.zzl_android.ui.main.LazyFragment;
import top.lyc829.zzl_android.ui.main.MainViewModel;
import top.lyc829.zzl_android.ui.message.MessageActivity;
import top.lyc829.zzl_android.ui.thing.dynamic.DynamicFragment;
import top.lyc829.zzl_android.ui.thing.follow.FellowFragment;
import top.lyc829.zzl_android.ui.thing.travels.TravelsFragment;

public class ThingFragment extends LazyFragment {

    private ThingViewModel mViewModel;
    private MainViewModel mainViewModel;
    private ThingFragmentBinding binding;
    private int lastSelectedPosition = 0;
    private long lastClickTime = 0L;

    TitleBar.ImageAction imageAction;

    Badge badge = null;

    public static ThingFragment newInstance() {
        return new ThingFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = ThingFragmentBinding.inflate(getLayoutInflater());
        return binding.getRoot();
        //return inflater.inflate(R.layout.thing_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(requireActivity()).get(ThingViewModel.class);
        mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);

        imageAction = new TitleBar.ImageAction(R.drawable.ic_baseline_email_24) {
            @Override
            public void performAction(View view) {
                startActivity(new Intent(getActivity(), MessageActivity.class));
            }
        };
        binding.mTitleBarThing.disableLeftView().addAction(imageAction);

        badge = new BadgeView(getContext())
                .bindTarget(binding.mTitleBarThing.getViewByAction(imageAction))
                .setBadgeGravity(Gravity.TOP | Gravity.END)
                .setExactMode(false)
                .setBadgeBackgroundColor(0xdfFFC364)
                .setGravityOffset(-3, 8, true)
                .setBadgePadding(1, true)
                .setBadgeNumber(0);

    }


    @Override
    public void lazyInit() {
        ViewPager2 viewPager2 = binding.thingViewpager;
        viewPager2.setAdapter(new FragmentStateAdapter(getActivity()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return new DynamicFragment();
                } else if (position == 1) {
                    return new TravelsFragment();
                } else if (position == 2) {
                    return new FellowFragment();

                }
                return new  DynamicFragment();
            }

            @Override
            public int getItemCount() {
                return 3;
            }
        });

        TabLayoutMediator mediator = new TabLayoutMediator(binding.easyIndicatorThing, viewPager2, ((tab, position) -> {
            if (position == 0) {
                tab.setText("动态");
            } else if (position == 1) {
                tab.setText("游记");
            } else if (position == 2) {
                tab.setText("关注");
            }
        }));

        mediator.attach();

        binding.easyIndicatorThing.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 切换tab时就更新该值，防止快速切换tab时触发双击刷新。
                lastSelectedPosition = tab.getPosition();

                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime > 1000) {
                    lastClickTime = currentTime;
                    return;
                }
                if (tab.getPosition() == lastSelectedPosition) {
                    switch (tab.getPosition()) {
                        case 0:
                            mViewModel.getDynamicDoubleClickLiveData().setValue(true);
                            mViewModel.getTravelsDoubleClickLiveData().setValue(false);
                            mViewModel.getFellowDoubleClickLiveData().setValue(false);
                            break;
                        case 1:
                            mViewModel.getDynamicDoubleClickLiveData().setValue(false);
                            mViewModel.getTravelsDoubleClickLiveData().setValue(true);
                            mViewModel.getFellowDoubleClickLiveData().setValue(false);
                            break;
                        case 2:
                            mViewModel.getDynamicDoubleClickLiveData().setValue(false);
                            mViewModel.getTravelsDoubleClickLiveData().setValue(false);
                            mViewModel.getFellowDoubleClickLiveData().setValue(true);
                            break;
                        default:
                            break;
                    }
                }
            }
        });


        msgNumberService();
    }

    @Override
    public void onResume() {
        super.onResume();
        //msgNumberService();
        mainViewModel.updateMsgNumber();
    }

    /**
     * 处理获取消息个数的服务
     */
    private void msgNumberService() {
        // 观察消息个数
        mainViewModel.getMsgNumberLiveData().observe(getViewLifecycleOwner(), msgNumberBean -> {
            badge.setBadgeNumber(msgNumberBean.getTotalNum());
        });
    }


}